package com.shanks.home.module.model;

import com.shanks.home.R;
import com.shanks.home.util.model.DataObserver;
import com.shanks.home.util.model.DataProvider;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class DataActivity extends Activity {
	
	private Button changeDataBtn;
	private TextView contentTv;
	private int i = 1;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		this.setContentView(R.layout.data_activity);
		
		initView();
		addDataObserver();
	}
	
	private void initView() {
		changeDataBtn = (Button)this.findViewById(R.id.data_change_data_btn);
		contentTv = (TextView)this.findViewById(R.id.data_content_tv);
		
		changeDataBtn.setOnClickListener(onClickListener);
	}
	
	private OnClickListener onClickListener = new OnClickListener() {
		
		@Override
		public void onClick(View v) {
			if (v == changeDataBtn) {
				DataProvider.getInstance().setData("test", i++);
			}
		}
	};
	
	private void addDataObserver() {
		DataProvider.getInstance().registerDataObserver("test", new MyDataObserver());
	}
	
	private class MyDataObserver extends DataObserver {
		@Override
		public void onChange(Integer value) {
//			super.onChange();
			contentTv.setText(String.valueOf(value));
		}
	}
}